import {
    InputType,
    StrategyKey,
    generateRandomId,
    getDomList,
    generateClassToId,
    removeErrorClass
} from '@/utils';
import type { BaseTableConfig } from '@/components/base-ui/base-table/type';
import type { StrategyParams } from '@/utils';

function getRuleFn(key: string) {
    return (params: StrategyParams) => {
        const matchProductionType = '自制';
        const currentTableData = params.tableInstance.data[params.row!];
        const validErrEl = getDomList(
            `.${generateClassToId(params.id!)}`,
            params.tableInstance.$el as HTMLElement
        )?.[params.row!];

        // 生产类型不为自制类型该字段必填
        if (currentTableData.productionType !== matchProductionType) {
            if (!currentTableData[key]) return '请完善必填信息';
        } else {
            removeErrorClass(validErrEl!);
        }
    };
}

export const tableConfig: BaseTableConfig = {
    items: [
        {
            modelKey: 'materialCode',
            columnProps: {
                label: '物料编码',
                width: 200
            },
            elInputProps: {
                maxlength: '50',
                showWordLimit: true
            },
            validProps: {
                id: generateRandomId(),
                required: true
            },
            validFormatType: InputType.NUMERIC_ALPHABET_SYMBOLS
        },
        {
            modelKey: 'materialName',
            columnProps: {
                label: '物料名称',
                width: 200
            },
            elInputProps: {
                maxlength: '50',
                showWordLimit: true
            },
            validProps: {
                id: generateRandomId(),
                required: true
            }
        },
        {
            itemType: 'select',
            slotName: 'materialClassificationSlot',
            modelKey: 'materialClassificationId',
            columnProps: {
                label: '物料分类',
                width: 200
            },
            validProps: {
                id: generateRandomId(),
                required: true
            }
        },
        {
            itemType: 'select',
            modelKey: 'productionType',
            elSelectProps: {
                options: [
                    {
                        label: '自制',
                        value: '自制'
                    },
                    {
                        label: '委外',
                        value: '委外'
                    },
                    {
                        label: '外购',
                        value: '外购'
                    }
                ]
            },
            columnProps: {
                label: '生产类型',
                width: 200
            },
            validProps: {
                id: generateRandomId(),
                required: true
            }
        },
        {
            modelKey: 'materialSpecification',
            columnProps: {
                label: '物料规格',
                width: 200
            },
            elInputProps: {
                maxlength: '50',
                showWordLimit: true
            },
            validProps: {
                id: generateRandomId()
            }
        },
        {
            itemType: 'select',
            modelKey: 'manufacturingStrategy',
            elSelectProps: {
                options: [
                    {
                        label: 'MTO',
                        value: 'MTO'
                    },
                    {
                        label: 'MTS',
                        value: 'MTS'
                    }
                ]
            },
            columnProps: {
                label: '制造策略',
                width: 200
            },
            validProps: {
                id: generateRandomId(),
                required: true
            }
        },
        {
            itemType: 'number',
            modelKey: 'inventory',
            columnProps: {
                label: '库存',
                width: 180
            },
            elInputNumberProps: {
                placeholder: '请输入',
                min: 0,
                max: 9999999999.99,
                precision: 2,
                controls: false
            },
            validProps: {
                id: generateRandomId(),
                required: true
            }
        },
        {
            itemType: 'number',
            modelKey: 'safetyInventory',
            default: 0,
            columnProps: {
                label: '安全库存',
                width: 180
            },
            elInputNumberProps: {
                placeholder: '请输入',
                min: 0,
                max: 9999999999.99,
                precision: 2,
                controls: false
            },
            validProps: {
                id: generateRandomId(),
                required: true
            }
        },
        {
            itemType: 'number',
            modelKey: 'fixedLeadTime',
            default: 1,
            columnProps: {
                label: '固定提前期',
                width: 180
            },
            otherConfig: {
                tip: true,
                tipMsg: '生产类型为外购和委外则该项为必填项'
            },
            elInputNumberProps: {
                placeholder: '请输入',
                min: 1,
                max: 9999999999,
                precision: 0,
                controls: false
            },
            validProps: {
                id: generateRandomId()
            },
            validRules: [
                {
                    validType: StrategyKey.IS_CUSTOMIZE,
                    ruleFn: getRuleFn('fixedLeadTime')
                }
            ]
        },
        {
            itemType: 'number',
            modelKey: 'fixedLeadTimeBatch',
            columnProps: {
                label: '固定提前期批量',
                width: 180
            },
            otherConfig: {
                tip: true,
                tipMsg: '生产类型为外购和委外则该项为必填项；外购类型的物料请按照采购单位填写批量；委外类型的物料请按照生产单位填写批量'
            },
            elInputNumberProps: {
                placeholder: '请输入',
                min: 1,
                max: 9999999999,
                precision: 0,
                controls: false
            },
            validProps: {
                id: generateRandomId()
            },
            validRules: [
                {
                    validType: StrategyKey.IS_CUSTOMIZE,
                    ruleFn: getRuleFn('fixedLeadTimeBatch')
                }
            ]
        },
        {
            itemType: 'number',
            modelKey: 'finishedProductRate',
            default: 100,
            elInputNumberProps: {
                min: 1,
                max: 100,
                precision: 2
            },
            columnProps: {
                label: '成品率(%)',
                width: 180
            },
            validProps: {
                id: generateRandomId(),
                required: true
            }
        },
        {
            modelKey: 'material',
            columnProps: {
                label: '材质',
                width: 200
            },
            elInputProps: {
                maxlength: '20',
                showWordLimit: true
            },
            validProps: {
                id: generateRandomId()
            }
        },
        {
            slotName: 'productUnit',
            modelKey: 'productUnit',
            columnProps: {
                label: '生产单位',
                width: 200
            },
            validProps: {
                id: generateRandomId(),
                required: true
            }
        },
        {
            slotName: 'purchaseUnit',
            modelKey: 'purchaseUnit',
            columnProps: {
                label: '采购单位',
                width: 160
            },
            otherConfig: {
                tip: true,
                tipMsg: '生产类型为外购和委外则该项为必填项'
            },
            validProps: {
                id: generateRandomId()
            },
            validRules: [
                {
                    validType: StrategyKey.IS_CUSTOMIZE,
                    ruleFn: getRuleFn('purchaseUnit')
                }
            ]
        }
    ],
    unAction: true
};
